package com.xwaf.platform.system.auth.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.xwaf.platform.common.api.constants.RedisKeys;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.redis.util.RedisUtil;
import com.xwaf.platform.common.service.service.CrudService;
import com.xwaf.platform.system.api.entity.auth.PermissionField;
import com.xwaf.platform.system.api.entity.auth.PermissionModule;
import com.xwaf.platform.system.api.entity.auth.PermissionOperation;
import com.xwaf.platform.system.api.service.auth.PermissionModuleService;
import com.xwaf.platform.system.api.service.auth.UserPermissionService;
import com.xwaf.platform.system.auth.dao.PermissionFieldDao;
import com.xwaf.platform.system.auth.dao.PermissionModuleDao;
import com.xwaf.platform.system.auth.dao.PermissionOperationDao;

/**
 * 
 * <p>
 * 模块表
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019-5-25 12:36:29 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@Transactional(readOnly = true)
@Service("permissionModuleService")
public class PermissionModuleServiceImpl extends CrudService<PermissionModuleDao, PermissionModule>
		implements PermissionModuleService {

	@Autowired
	private PermissionModuleDao permissionModuleDao;
	@Autowired
	private PermissionFieldDao permissionFieldDao;
	@Autowired
	private PermissionOperationDao permissionOperationDao;
	@Autowired
	private UserPermissionService userPermissionService;
	@Autowired
	private RedisUtil redisUtil;

	@Override
	public Page<PermissionModule> selPageList(Page<PermissionModule> page, PermissionModule permissionModule) {
		List<PermissionModule> list = permissionModuleDao.selPageList(page, permissionModule);
		page.setRecords(list);
		return page;
	}

	@Override
	public List<String> findAllModuleCode() {
		return permissionModuleDao.selectAllCode();
	}

	@Override
	public List<String> findAllFieldCode() {
		return permissionFieldDao.selectAllCode();
	}

	@Override
	public List<String> findAllOperationCode() {
		return permissionOperationDao.selectAllCode();
	}

	@Override
	public List<PermissionModule> findDistributionTemplateByUserId(ELUser loginUser) {
		// 1.PermissionModule 数据变更时需要移除缓存(未处理) TODO
		// 2.PermissionOperation 数据变更时需要移除缓存(未处理) TODO
		// 3.PermissionField 数据变更时需要移除缓存(未处理) TODO
		// 4.UserPermission 数据变更时需要移除缓存(已处理)
		if (redisUtil.exists(RedisKeys.PERMISSION_MODULE)) {
			return (List<PermissionModule>) redisUtil.get(RedisKeys.PERMISSION_MODULE);
		}
		// 先根据用户id查询出对应的权限信息
		List<String> plist = userPermissionService.findUserAllPermissionCodes(loginUser.getId());
		List<PermissionModule> mlist = permissionModuleDao.selectAll(null);// 需要存入缓存
		List<PermissionOperation> olist = permissionOperationDao.selectAll();
		List<PermissionField> flist = permissionFieldDao.selectAll();
		return isChecked(mlist, plist, olist, flist);
	}

	/**
	 * <p>
	 * 递归算法,判断用户拥有得权限
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2020年1月3日 下午2:37:23 <br>
	 * @see 无<br>
	 * @param mlist
	 * @param plist
	 * @param olist
	 * @param flist
	 * @return
	 * @demo <br>
	 */
	private List<PermissionModule> isChecked(List<PermissionModule> mlist, List<String> plist,
			List<PermissionOperation> olist, List<PermissionField> flist) {
		for (PermissionModule module : mlist) {
			List<PermissionField> newFlist = new ArrayList<PermissionField>();
			List<PermissionOperation> newOlist = new ArrayList<PermissionOperation>();
			// 设模块置选中项
			if (null != plist && plist.size() > 0) {
				for (String code : plist) {
					if (module.getCode().equals(code)) {
						module.setChecked(true);
					}
				}
			}
			for (PermissionOperation operation : olist) {
				// 设模选中操作
				if (null != plist && plist.size() > 0) {
					for (String code : plist) {
						if (operation.getCode().equals(code)) {
							operation.setChecked(true);
						}
					}
				}
				// 将模块下所有操作放入模块中
				if (operation.getModuleCode().equals(module.getCode())) {
					newOlist.add(operation);
				}
			}
			module.setPermissionOperations(newOlist);
			for (PermissionField filed : flist) {
				// 设模选中敏感字段
				if (null != plist && plist.size() > 0) {
					for (String code : plist) {
						if (filed.getCode().equals(code)) {
							filed.setChecked(true);
						}
					}
				}
				// 将模块下所有敏感字段放入模块中
				if (filed.getModuleCode().equals(module.getCode())) {
					newFlist.add(filed);
				}
			}
			module.setPermissionFields(newFlist);
			if (module.getPermissionModules() != null && module.getPermissionModules().size() > 0) {
				this.isChecked(module.getPermissionModules(), plist, olist, flist);
			}
		}
		// 1分钟 = 60秒
		// 60 * 60 = 1H
		// 1H * 12 = 12h
		redisUtil.set(RedisKeys.PERMISSION_MODULE, mlist, 60 * 60 * 12);
		return mlist;
	}

}
